
// 设置cookie
function setCookie(name, value, days = 7) {
    var date = new Date();
    var hours = date.getHours();
    date.setHours(hours - 8);
    date.setDate(date.getDate() + days);
    document.cookie = `${name}=${value};path=/;expires=${date}`;
}

function getCookie(name) {
    var cookies = document.cookie;
    var arr = cookies.split('; ');
    arr = arr.map(val => {
        return {
            name: val.split('=')[0],
            value: val.split('=')[1],
        }
    })
    var res = arr.filter(val => val.name === name)[0];
    console.log(res)
    if (res) {
        return res.value
    } else {
        return ''
    }
}


// 查询地址栏的数据参数
function queryString(name) {
    var href = location.search.replace('?', '');
    var arr = href.split('&');
    arr = arr.map(val => {
        return {
            name: val.split('=')[0],
            value: val.split('=')[1]
        }
    })
    var res = arr.filter(val => val.name === name)[0];
    if (res) {
        return res.value
    } else {
        return ''
    }
}




function request(data, type, path) {
    return new Promise((resolve, reject) => {

        if (type === 'get') {
            $.get(
                path,
                data,
                function (data) {
                    resolve(data)
                },
                'json'
            )
        } else {
            $.post(
                path,
                data,
                function (data) {
                    resolve(data)
                },
                'json'
            )
        }

    })
}

// const login = function (data) {
//     return request(data, 'post', 'http://192.168.56.23/2104/day24/php/login.php')
// }



// 登录
const login = data => request(data, 'get', '../php/login.php')

// 注册
const reg = data => request(data, 'post', '../php/reg.php')

// 显示商品列表
const goods_list = data => request(data, 'get', '../php/list.php')

// 显示商品详情
const goods_detail = data => request(data, 'get', '../php/detail.php')

// 添加到购物车
const addCart = data => request(data, 'post', '../php/addCart.php')

// 显示购物车
const showCart = data => request(data, 'get', '../php/showCart.php')

// 删除购物车
const delCart = data => request(data, 'post', '../php/delCart.php')
//加减物品数量
const changeNum = data =>request(data,'post','../php/changenum.php')






